# !/usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import redirect
# 这是这个中间件的类
from django.utils.deprecation import MiddlewareMixin


class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):

        # 0. 排除哪些不需要登陆就能访问的页面
        # request.path_info 获取当前用户请求的URL
        if request.path_info == "/mytest/login/":
            return
        elif request.path_info == "/mytest/admin/":
            print("你在访问admin")
            if request.session.get("info")["name"] != "admin":
                return redirect("/mytest/chart/")
            print(request.session.get("info")["name"])

        # 1. 读取当前访问的用户的session信息，如果能读到，说明已登录过，就可以继续向后走
        info_dict = request.session.get("info")
        print(info_dict)
        # print("进来了")
        if info_dict:
            return

        # 2. 没有登陆过，重新回到登陆界面
        return redirect("/mytest/login/")
